import { loadBase64 } from './util.js';
import { BaseTexture, Texture, AnimatedSprite } from 'pixi.js'

export async function clipMob(resource, mobId, behavior) {
    const Mob = resource.Content.Mob[mobId][behavior];
    const length = Object.keys(Mob).length;
    const Textures = [];
    let animationSprite = null;

    for (let i = 0; i < length; i ++) {
        if (typeof Mob[i] !== 'object') {
            continue;
        }
        let mob = Mob[i];
        let mobImage = await loadBase64(mob.value);
        let base = new BaseTexture(mobImage);
        let texture = new Texture(base);
        texture.defaultAnchor.x = (mob.origin.x) / mobImage.width;
        texture.defaultAnchor.y = (mob.origin.y) / mobImage.height;
        Textures.push(texture);
    }
    // 由于animationSpirte中textures只有一个texture时，不会触发onComplete钩子。
    if (Textures.length === 1) {
        Textures.push(Textures[0].clone());
    }
    animationSprite = new AnimatedSprite(Textures);
    animationSprite.zIndex = 100000;
    return animationSprite;

}